package com.example.carbonbackend.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.carbonbackend.common.BaseResponse;
import com.example.carbonbackend.common.ResultUtils;
import com.example.carbonbackend.model.dto.*;
import com.example.carbonbackend.model.entity.CarbonEmission;
import com.example.carbonbackend.model.entity.Company;
import com.example.carbonbackend.service.CarbonEmissionService;
import com.example.carbonbackend.service.CompanyService;
import com.example.carbonbackend.service.OssUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* @author 25103
* @description 针对表【carbonemission(碳排放表)】的数据库操作Service实现
* @createDate 2024-03-11 16:34:28
*/
@Service
public class CarbonemissionService1Impl {

    @Autowired
    private OssUploadService ossUploadService;
    @Autowired
    private CompanyService companyService;

    @Autowired
    private CarbonEmissionService carbonemissionService;


    /**
     * 发电企业碳排放数据计算过程
     */

    public BaseResponse<String> accountForEG(EGCarbonemissionDto egCarbonemissionDto) throws IOException {
        Double eBurn=0.0;
        Double eDesul=0.0;
        Double eElec=0.0;
        Double e=0.0;
        for(FossilFuelDto fossilFuel:egCarbonemissionDto.getFossilFuelList()){
            eBurn+=fossilFuel.getFC()* fossilFuel.getNCV()*1e-6* fossilFuel.getCC()
                    * fossilFuel.getOF()*44.0/12.0;
        }
        for(DesulfurizerDto desulfurizer:egCarbonemissionDto.getDesulfurizerList()){
            eDesul+=desulfurizer.getI()*desulfurizer.getEFkt();
        }
        eElec= egCarbonemissionDto.getEFE()* egCarbonemissionDto.getADE();
        e=eBurn+eDesul+eElec;
        egCarbonemissionDto.setEBurn(eBurn);
        egCarbonemissionDto.setEDesul(eDesul);
        egCarbonemissionDto.setEElec(eElec);
        egCarbonemissionDto.setE(e);
        List<EGReport> reports=new ArrayList<EGReport>();
        reports.add(new EGReport("企业二氧化碳排放总量（tCO2）",e));
        reports.add(new EGReport("化石燃料燃烧排放量（tCO2）",eBurn));
        reports.add(new EGReport("脱硫过程排放量（tCO2））",eDesul));
        reports.add(new EGReport("净购入使用的电力排放量（tCO2）",eElec));


        String url= null;
        try {
            url = WordWriter.createEGWordDocument(egCarbonemissionDto.getCompanyName(),
                    egCarbonemissionDto.getYear(),e,eBurn,eDesul,eElec );
        } catch (IllegalStateException ex) {
            throw new IllegalStateException("上传格式错误，请重新上传");
        }

        LambdaQueryWrapper<Company> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getCompanyName,egCarbonemissionDto.getCompanyName());
        Company company= companyService.getOne(queryWrapper);
        CarbonEmission carbonemission = new CarbonEmission();
        carbonemission.setCompanyId(company.getId());
        Long l= 1L;
        carbonemission.setUrl(url);
        carbonemissionService.save(carbonemission);
        return ResultUtils.success("上传成功");
    }
    /**
     * 接收表现层传入的碳排放数据
     */
    public BaseResponse<String> inputEG(String name, String year, File file) throws IOException {
       // com.example.carbonbackend.service.CarbonemissionService1 carbonemissionService=new CarbonemissionService1Impl();
        EGCarbonemissionDto egCarbonemissionDto= WordReader.eGAssignment(name,year,file);
        return  accountForEG(egCarbonemissionDto);
    }


    public EUCarbonemissionDto accountForEU(EUCarbonemissionDto euCarbonemissionDto) {

        return null;
    }
}




